var e = require("../@babel/runtime/helpers/interopRequireDefault").default;

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.default = void 0;

var n = require("../@babel/runtime/helpers/classCallCheck"), t = require("../@babel/runtime/helpers/createClass"), i = require("../@babel/runtime/helpers/inherits"), o = require("../@babel/runtime/helpers/createSuper"), a = e(require("./index")), u = e(require("../api/wxRequest")), r = require("../utils/util.js"), c = e(require("../utils/Tips")), l = getApp(), s = "/auc/v0/miniAuth", f = 0, h = function(e) {
    i(h, e);
    var a = o(h);
    function h() {
        return n(this, h), a.apply(this, arguments);
    }
    return t(h, null, [ {
        key: "request",
        value: function() {
            var e = this, n = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {}, t = n.method, i = void 0 === t ? "POST" : t, o = n.baseUrl, a = void 0 === o ? l.globalData.baseUrl : o, h = n.url, d = void 0 === h ? "" : h, v = n.data, k = void 0 === v ? {} : v, m = n.token, g = void 0 === m || m, p = n.contentType, A = void 0 === p ? "application/json" : p, b = n.header, _ = void 0 === b ? {} : b, w = function n() {
                return new Promise(function(t, o) {
                    var l = g ? e.createAuthHeader() : {}, h = e.createAucCookieHeader(), v = "".concat(a).concat(d);
                    wx.request({
                        url: v,
                        method: i,
                        header: (0, r.merge)({
                            "content-type": A
                        }, _, l, h),
                        data: k,
                        success: function(i) {
                            var a = i.statusCode, r = e;
                            if (200 !== a) c.default.modal("服务器崩溃啦~请联系管理人员"), e.handleHttpException(i), o(i); else {
                                var l = i.data, h = l.code;
                                if ("10004" === h) u.default.wxLogin().then(function(n) {
                                    return e.login(n, g);
                                }).then(function(t) {
                                    return e.saveToken(t), n();
                                }).then(function(e) {
                                    t(e);
                                }).catch(function(e) {
                                    o(e);
                                }); else if (d === s && "30010" === h) {
                                    if (f > 0) return o(), !0;
                                    f++, wx.showModal({
                                        content: "没有获取到您的信息，请刷新当前页面后再分享",
                                        confirmText: "刷新",
                                        success: function(e) {
                                            e.confirm && r.loginAndAuc(g).then(function(e) {
                                                return n();
                                            }).then(function(e) {
                                                t(e);
                                            }).catch(function(e) {
                                                o(e);
                                            });
                                        }
                                    });
                                } else if (d === s && "-1" === h) {
                                    if (f > 0) return o(), !0;
                                    f++, wx.showModal({
                                        content: "没有获取到您的信息，请刷新当前页面后再分享",
                                        confirmText: "刷新",
                                        success: function(e) {
                                            e.confirm && r.loginAndAuc(g).then(function(e) {
                                                return n();
                                            }).then(function(e) {
                                                t(e);
                                            }).catch(function(e) {
                                                o(e);
                                            });
                                        }
                                    });
                                } else {
                                    if (d === s) {
                                        l.code;
                                        var v = l.result;
                                        v && v["auc-cookie"] && e.save_miniAuth_aucCookie(v["auc-cookie"]);
                                    }
                                    t(l);
                                }
                            }
                        },
                        fail: function(e) {
                            console.error("网络请求发起失败", e), c.default.modal("网络错误"), o(e);
                        }
                    });
                });
            };
            return g && !l.globalData.aucMiniAuth.sid ? new Promise(function(n, t) {
                e.loginAndAuc(g).then(function() {
                    return w();
                }).then(function(e) {
                    n(e);
                }).catch(function(e) {
                    t(e);
                });
            }) : w();
        }
    }, {
        key: "loginAndAuc",
        value: function(e) {
            var n = this;
            return new Promise(function(t, i) {
                u.default.wxLogin().then(function(t) {
                    return n.login(t, e);
                }).then(function(e) {
                    return n.saveToken(e);
                }).then(function() {
                    return n.auth(e);
                }).then(function(e) {
                    n.save_miniAuth_sid(e), t();
                }).catch(function(e) {
                    i(e);
                });
            });
        }
    }, {
        key: "auth",
        value: function(e) {
            var n = this;
            return new Promise(function(e, t) {
                n.get("/auc/v0/miniAuth", {}, {
                    token: !1
                }).then(function(n) {
                    var t = n.code, i = n.result;
                    "0" === t && e(i.token);
                }).catch(function(e) {
                    t(e);
                });
            });
        }
    }, {
        key: "login",
        value: function(e, n) {
            var t = this;
            return new Promise(function(i, o) {
                var a = t.lastLoginTime, u = void 0 === a, r = Date.now();
                t.lastLoginTime = r;
                if (!u && r - a <= 1e4) {
                    var c = "tokenInverval" + Date.now();
                    t[c] = setInterval(function() {
                        var e = t.getToken();
                        e && e !== n && (clearInterval(t[c]), i({
                            data: {
                                mini_program_cookie: t.getToken()
                            }
                        }));
                    }, 300);
                } else t.get("/KCard/chg-assist/user/login", {
                    code: e
                }, {
                    token: !1
                }).then(function(e) {
                    console.log("微信鉴权成功", e), "0" === e.code && (l.globalData.aucPhone = e.data.phone), 
                    i(e);
                }).catch(function(e) {
                    o(e);
                });
            });
        }
    }, {
        key: "save_miniAuth_sid",
        value: function(e) {
            l.globalData.aucMiniAuth.sid = e;
        }
    }, {
        key: "get_miniAuth_sid",
        value: function() {
            return l.globalData.aucMiniAuth.sid;
        }
    }, {
        key: "save_miniAuth_aucCookie",
        value: function(e) {
            l.globalData.aucMiniAuth.aucCookie = e;
        }
    }, {
        key: "createAucCookieHeader",
        value: function() {
            var e = l.globalData.token, n = l.globalData.aucMiniAuth, t = n.sid, i = n.aucCookie, o = n.a, a = {};
            return a.a = o, t && (a.sid = "".concat(t)), e && (a.Cookie = "mini_program_cookie=".concat(e)), 
            i && (a.Cookie = "auc-cookie=".concat(i)), i && e && (a.Cookie = "mini_program_cookie=".concat(e, ";auc-cookie=").concat(i)), 
            a;
        }
    } ]), h;
}(a.default);

exports.default = h;